package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectMilepostEntity;
import com.topscomm.gpm.pojo.GpmProjectTaskEntity;
import com.topscomm.gpm.service.impl.GpmProjectMilepostService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.gpm.service.impl.GpmProjectTaskService;
import com.topscomm.gpm.utils.MessageUtil;
import com.topscomm.mis.constant.MisSystemConst;
import com.topscomm.mis.mobile.message.service.SendMessageService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;

import java.util.Map;

import com.topscomm.tap.common.DatacodeCache;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wudaliang
 * @date 2021/9/16
 * @description:  下发任务给责任人，给责任人发送提醒
 * @modify:
 */
public class GpmProjectTaskScript {

    @Transactional(rollbackFor = Exception.class)
    public void remindResponsible(ITapService service, TapEntity entity){
        // 审批节点实例
        CboApprovalNodeInsEntity nodeInsEntity = (CboApprovalNodeInsEntity) entity;
//        ICboMessageAppService cboMessageAppService = (ICboMessageAppService) SpringUtil.getBean("cboMessageAppService");
//        ICboMessageSysService cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        SendMessageService sendMessageService= (SendMessageService) SpringUtil.getBean("misPushMsgService");
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String sourceid = ConvertUtil.convertToString(nodeInsEntity.getSourceid());
        GpmProjectTaskService gpmProjectTaskService = (GpmProjectTaskService) SpringUtil
            .getBean("gpmProjectTaskService");
        GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil
            .getBean("gpmProjectService");
        GpmProjectMilepostService gpmProjectMilepostService = (GpmProjectMilepostService) SpringUtil
            .getBean("gpmProjectMilepostService");
        GpmProjectTaskEntity taskEntity = gpmProjectTaskService.queryById(sourceid);
        // 给任务责任人发送消息提醒
        String responsible = taskEntity.getResponsible();
        String responsibleCode = taskEntity.getResponsiblecode();
        String projectCode = taskEntity.getProjectcode();
        GpmProjectEntity projectEntity = gpmProjectService.queryByWhere(" projectcode='" + projectCode + "' and enabled=1 ").get(0);
        StringBufferProxy string = new StringBufferProxy();
        // 判断项目名称末尾是否带项目二字
        String projectName = ConvertUtil.convertToString(projectEntity.getProjectname());
        String projectLevel = datacodeCache.getValueDynamic("GpmProjectLevel", projectEntity.getProjectlevel(),
                "code", "name");
        if ("项目".equals(projectName.substring(projectName.length() - 2))) {
            string.appendSingle("【{0} {1}级 {2}】 {3}任务已派发给您，请登录通用项目管理系统查阅通用任务信息。",
                    projectCode, projectLevel, projectName, taskEntity.getTaskname());
        } else {
            string.appendSingle("【{0} {1}级 {2}项目】 {3}任务已派发给您，请登录通用项目管理系统查阅通用任务信息。",
                    projectCode, projectLevel, projectName, taskEntity.getTaskname());
        }
        //App消息
        Map<String, Object> messageAppMap = MessageUtil.createMessageAppMapV1(responsibleCode, responsible, "通用管理项目任务提醒", string.toString(), Long.parseLong(sourceid), GpmProjectTaskEntity.tableName, 0);
        messageAppMap.put("messagetype", MisSystemConst.MobileMessageType.TASK_TYPE);
        sendMessageService.sendAppMessage(messageAppMap,ConvertUtil.convertToString(nodeInsEntity.getId()));
//        cboMessageAppService.insertMap(messageAppMap);

        //系统消息
//        Map<String, Object> messageSysMap= MessageUtil.createMessageSysMap(responsibleCode, responsible, "交付项目任务提醒", string.toString(), Long.parseLong(sourceid), GpmProjectTaskEntity.tableName);
//        cboMessageSysService.insertMap(messageSysMap);
        Map<String, Object> messageSysMap= MessageUtil.createMessageSysMapV1(responsibleCode, responsible, "通用管理项目任务提醒", string.toString(), Long.parseLong(sourceid), GpmProjectTaskEntity.tableName);
        //cboMessageSysService.insertMap(messageSysMap);
        messageSysMap.put("messagetype",MisSystemConst.MobileMessageType.TASK_TYPE);
        sendMessageService.sendSysMessage(messageSysMap,ConvertUtil.convertToString(nodeInsEntity.getId()));

        // 查询任务所属里程碑
        GpmProjectMilepostEntity gpmProjectMilepostEntity = gpmProjectMilepostService.queryById(taskEntity.getMilepostcode());// code字段保存的是id
        String mileResponsible = gpmProjectMilepostEntity.getResponsible();
        String mileResponsibleCode = gpmProjectMilepostEntity.getResponsiblecode();
        // 若任务责任人和里程碑责任人为同一个人则只需要发任务提醒
        if (mileResponsibleCode.equals(responsibleCode)) {
            return;
        }
        // 给里程碑责任人发送消息提醒
        string.clear();
        if ("项目".equals(projectName.substring(projectName.length() - 2))) {
            string.appendSingle("【{0} {1}级 {2}】您负责的{3}里程碑下的{4}任务已派发给{5}。", projectCode, projectLevel,
                    projectName, gpmProjectMilepostEntity.getMilepostname(), taskEntity.getTaskname(), responsible);
        } else {
            string.appendSingle("【{0} {1}级 {2}项目】您负责的{3}里程碑下的{4}任务已派发给{5}。", projectCode, projectLevel,
                    projectName, gpmProjectMilepostEntity.getMilepostname(), taskEntity.getTaskname(), responsible);
        }
        //App消息
        messageAppMap = MessageUtil.createMessageAppMapV1(mileResponsibleCode, responsible, "通用任务提醒", string.toString(),
                Long.parseLong(sourceid), GpmProjectTaskEntity.tableName, 0);
//        cboMessageAppService.insertMap(messageAppMap);
        messageAppMap.put("messagetype", MisSystemConst.MobileMessageType.TASK_TYPE);
        sendMessageService.sendAppMessage(messageAppMap,ConvertUtil.convertToString(nodeInsEntity.getId()));
        //系统消息
        messageSysMap= MessageUtil.createMessageSysMapV1(mileResponsibleCode, mileResponsible, "通用任务提醒", string.toString(),
                Long.parseLong(sourceid), GpmProjectTaskEntity.tableName);
//        cboMessageSysService.insertMap(messageSysMap);
        messageSysMap.put("messagetype",MisSystemConst.MobileMessageType.TASK_TYPE);
        sendMessageService.sendSysMessage(messageSysMap,ConvertUtil.convertToString(nodeInsEntity.getId()));
    }

}
